﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;

namespace helpcarla
{

    public partial class frmDailyLog : Form
    {
        int EnergyLevel = 0;
        int PainLevel = 0;

        public frmDailyLog()
        {
            InitializeComponent();
        }

        private bool checkValidM()
        {
            if (txtBE.Text == string.Empty)
            {
                MessageBox.Show("Please enter in what food you have eaten for breakfast");
                return false;
            }
            if (txtBD.Text == string.Empty)
            {
                MessageBox.Show("Please enter in what you have drinks you have had for breakfast");
                return false;
            }
            if (txtBM.Text == string.Empty)
            {
                MessageBox.Show("Please enter in what medicine you have taken with breakfast");
                return false;
            }
            if (cbBET.SelectedItem == "-- Time --")
            {
                MessageBox.Show("Please pick the time when you ate breakfast");
                return false;
            }
            if (cbBDT.SelectedItem == "-- Time --")
            {
                MessageBox.Show("Please pick the time when you drank during breakfast");
                return false;
            }
            if (cbBMT.SelectedItem == "-- Time --")
            {
                MessageBox.Show("Please pick the time when you took medicine with breakfast");
                return false;
            }
            if (txtLE.Text == string.Empty)
            {
                MessageBox.Show("Please enter in what food you have eaten for lunch");
                return false;
            }
            if (txtLD.Text == string.Empty)
            {
                MessageBox.Show("Please enter in what you have drinks you have had for lunch");
                return false;
            }
            if (txtLM.Text == string.Empty)
            {
                MessageBox.Show("Please enter in what medicine you have taken with lunch");
                return false;
            }
            if (cbLET.SelectedItem == "-- Time --")
            {
                MessageBox.Show("Please pick the time when you ate lunch");
                return false;
            }
            if (cbLDT.SelectedItem == "-- Time --")
            {
                MessageBox.Show("Please pick the time when you drank during lunch");
                return false;
            }
            if (cbLMT.SelectedItem == "-- Time --")
            {
                MessageBox.Show("Please pick the time when you took medicine with lunch");
                return false;
            }
            if (txtDE.Text == string.Empty)
            {
                MessageBox.Show("Please enter in what food you have eaten for dinner");
                return false;
            }
            if (txtDD.Text == string.Empty)
            {
                MessageBox.Show("Please enter in what you have drinks you have had for dinner");
                return false;
            }
            if (txtDM.Text == string.Empty)
            {
                MessageBox.Show("Please enter in what medicine you have taken with dinner");
                return false;
            }
            if (cbDET.SelectedItem == "-- Time --")
            {
                MessageBox.Show("Please pick the time when you ate dinner");
                return false;
            }
            if (cbDDT.SelectedItem == "-- Time --")
            {
                MessageBox.Show("Please pick the time when you drank during dinner");
                return false;
            }
            if (cbDMT.SelectedItem == "-- Time --")
            {
                MessageBox.Show("Please pick the time when you took medicine with dinner");
                return false;
            }
            return true;
        }

        private bool checkValidS()
        {
            if (cbFellAsleep.SelectedItem == "-- Time --")
            {
                MessageBox.Show("Please pick the time when you fell asleep");
                return false;
            }
            if (cbWakeUp.SelectedItem == "-- Time --")
            {
                MessageBox.Show("Please pick the time when you woke up");
                return false;
            }
            return true;
        }

        public bool checkLevels()
        {
            if (rdoE1.Checked == true)
            {
                EnergyLevel = 1;
            }
            if (rdoE2.Checked == true)
            {
                EnergyLevel = 2;
            }
            if (rdoE3.Checked == true)
            {
                EnergyLevel = 3;
            }
            if (rdoE4.Checked == true)
            {
                EnergyLevel = 4;
            }
            if (rdoE5.Checked == true)
            {
                EnergyLevel = 5;
            }
            if (rdoE6.Checked == true)
            {
                EnergyLevel = 6;
            }
            if (rdoE7.Checked == true)
            {
                EnergyLevel = 7;
            }
            if (rdoE8.Checked == true)
            {
                EnergyLevel = 8;
            }
            if (rdoE9.Checked == true)
            {
                EnergyLevel = 9;
            }
            if (rdoE10.Checked == true)
            {
                EnergyLevel = 10;
            }

            if (rdoP1.Checked == true)
            {
                PainLevel = 1;
            }
            if (rdoP2.Checked == true)
            {
                PainLevel = 2;
            }
            if (rdoP3.Checked == true)
            {
                PainLevel = 3;
            }
            if (rdoP4.Checked == true)
            {
                PainLevel = 4;
            }
            if (rdoP5.Checked == true)
            {
                PainLevel = 5;
            }
            if (rdoP6.Checked == true)
            {
                PainLevel = 6;
            }
            if (rdoP7.Checked == true)
            {
                PainLevel = 7;
            }
            if (rdoP8.Checked == true)
            {
                PainLevel = 8;
            }
            if (rdoP9.Checked == true)
            {
                PainLevel = 9;
            }
            if (rdoP10.Checked == true)
            {
                PainLevel = 10;
            }

            if (EnergyLevel == 0)
            {
                MessageBox.Show("Please select an energy level");
                return false;
            }
            if (PainLevel == 0)
            {
                MessageBox.Show("Please select a pain level");
                return false;
            }
            return true;
        }

        private bool checkValidMood()
        {
            if (cbMoodChanges.SelectedItem == "-- Mood --")
            {
                MessageBox.Show("Please choose the mood that you are currently in");
                return false;
            }
            return true;
        }

        private void frmFoodLog_Load(object sender, EventArgs e)
        {
            tabMealInfo.BackColor = SystemColors.Control;
            tabSleepHabits.BackColor = SystemColors.Control;
            tabEmotionalLevels.BackColor = SystemColors.Control;
            tabMoodChanges.BackColor = SystemColors.Control;
            cbBET.Text = "-- Time --";
            cbBDT.Text = "-- Time --";
            cbBMT.Text = "-- Time --";
            cbDET.Text = "-- Time --";
            cbDDT.Text = "-- Time --";
            cbDMT.Text = "-- Time --";
            cbLET.Text = "-- Time --";
            cbLDT.Text = "-- Time --";
            cbLMT.Text = "-- Time --";
            cbFellAsleep.Text = "-- Time --";
            cbWakeUp.Text = "-- Time --";
            cbMoodChanges.Text = "-- Mood --";

            if (tabcontrolDailyLog.SelectedTab == tabEmotionalLevels)
            {
                this.Size = new Size(434, 339);
                tabcontrolDailyLog.Size = new Size(420, 305);
            }

            if (tabcontrolDailyLog.SelectedTab == tabMealInfo)
            {
                this.Size = new Size(396, 569);
                tabcontrolDailyLog.Size = new Size(381, 536);
            }

            if (tabcontrolDailyLog.SelectedTab == tabMoodChanges)
            {
                this.Size = new Size(330, 157);
                tabcontrolDailyLog.Size = new Size(313, 119);
            }

            if (tabcontrolDailyLog.SelectedTab == tabSleepHabits)
            {
                this.Size = new Size(372, 269);
                tabcontrolDailyLog.Size = new Size(356, 233);
            }
        }

        private void tabcontrolDailyLog_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (tabcontrolDailyLog.SelectedTab == tabEmotionalLevels)
            {
                this.Size = new Size(434, 339);
                tabcontrolDailyLog.Size = new Size(420, 305);
            }

            if (tabcontrolDailyLog.SelectedTab == tabMealInfo)
            {
                this.Size = new Size(396, 569);
                tabcontrolDailyLog.Size = new Size(381, 536);
            }

            if (tabcontrolDailyLog.SelectedTab == tabMoodChanges)
            {
                this.Size = new Size(330, 157);
                tabcontrolDailyLog.Size = new Size(313, 119);
            }

            if (tabcontrolDailyLog.SelectedTab == tabSleepHabits)
            {
                this.Size = new Size(372, 269);
                tabcontrolDailyLog.Size = new Size(356, 233);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (checkValidM())
            {
                tabcontrolDailyLog.SelectTab(tabSleepHabits);
            }
        }

        private void btnNext2_Click(object sender, EventArgs e)
        {
            if (checkValidS())
            {
                tabcontrolDailyLog.SelectTab(tabEmotionalLevels);
            }
        }

        private void btnNext3_Click(object sender, EventArgs e)
        {
            if (checkLevels())
            {
                tabcontrolDailyLog.SelectTab(tabMoodChanges);
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            tabcontrolDailyLog.SelectTab(tabMealInfo);
        }

        private void btnBack2_Click(object sender, EventArgs e)
        {
            tabcontrolDailyLog.SelectTab(tabSleepHabits);
        }

        private void btnBack3_Click(object sender, EventArgs e)
        {
            tabcontrolDailyLog.SelectTab(tabEmotionalLevels);
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (checkValidM() == true && checkValidS() == true && checkLevels() == true && checkValidMood() == true)
            {
                try
                {
                    frmMain.connectDB().Open();
                    string insertBQuery = "INSERT INTO DailyFoodLog (Food, Drink, Medicine, Meal, Date, FoodTime, DrinkTime, MedicineTime) VALUES (";
                    insertBQuery += "\'" + txtBE.Text + "\'" + ", ";
                    insertBQuery += "\'" + txtBD.Text + "\'" + ", ";
                    insertBQuery += "\'" + txtBM.Text + "\'" + ", ";
                    insertBQuery += "\'" + lblBreakfast.Text + "\'" + ", ";
                    insertBQuery += "\'" + DateTime.Now.ToString("yyyy-MM-dd") + "\'" + ", ";
                    insertBQuery += "\'" + cbBET.Text + "\'" + ", ";
                    insertBQuery += "\'" + cbBDT.Text + "\'" + ", ";
                    insertBQuery += "\'" + cbBMT.Text + "\'" + ") ";
                    MySql.Data.MySqlClient.MySqlCommand bCommand = new MySql.Data.MySqlClient.MySqlCommand(insertBQuery, frmMain.connectDB());

                    string insertLQuery = "INSERT INTO DailyFoodLog (Food, Drink, Medicine, Meal, Date, FoodTime, DrinkTime, MedicineTime) VALUES (";
                    insertLQuery += "\'" + txtLE.Text + "\'" + ", ";
                    insertLQuery += "\'" + txtLD.Text + "\'" + ", ";
                    insertLQuery += "\'" + txtLM.Text + "\'" + ", ";
                    insertLQuery += "\'" + lblLunch.Text + "\'" + ", ";
                    insertLQuery += "\'" + DateTime.Now.ToString("yyyy-MM-dd") + "\'" + ", ";
                    insertLQuery += "\'" + cbLET.Text + "\'" + ", ";
                    insertLQuery += "\'" + cbLDT.Text + "\'" + ", ";
                    insertLQuery += "\'" + cbLMT.Text + "\'" + ") ";
                    MySql.Data.MySqlClient.MySqlCommand lCommand = new MySql.Data.MySqlClient.MySqlCommand(insertLQuery, frmMain.connectDB());

                    string insertDQuery = "INSERT INTO DailyFoodLog (Food, Drink, Medicine, Meal, Date, FoodTime, DrinkTime, MedicineTime) VALUES (";
                    insertDQuery += "\'" + txtDE.Text + "\'" + ", ";
                    insertDQuery += "\'" + txtDD.Text + "\'" + ", ";
                    insertDQuery += "\'" + txtDM.Text + "\'" + ", ";
                    insertDQuery += "\'" + lblDinner.Text + "\'" + ", ";
                    insertDQuery += "\'" + DateTime.Now.ToString("yyyy-MM-dd") + "\'" + ", ";
                    insertDQuery += "\'" + cbDET.Text + "\'" + ", ";
                    insertDQuery += "\'" + cbDDT.Text + "\'" + ", ";
                    insertDQuery += "\'" + cbDMT.Text + "\'" + ") ";
                    MySql.Data.MySqlClient.MySqlCommand dCommand = new MySql.Data.MySqlClient.MySqlCommand(insertDQuery, frmMain.connectDB());
                    bCommand.ExecuteNonQuery();
                    lCommand.ExecuteNonQuery();
                    dCommand.ExecuteNonQuery();

                    string insertSQuery = "INSERT INTO SleepHabits (FellAsleepTime, WokeUpTime) VALUES(";
                    insertSQuery += "\'" + cbFellAsleep.Text + "\'" + ", ";
                    insertSQuery += "\'" + cbWakeUp.Text + "\'" + ") ";
                    MySql.Data.MySqlClient.MySqlCommand sCommand = new MySql.Data.MySqlClient.MySqlCommand(insertSQuery, frmMain.connectDB());
                    sCommand.ExecuteNonQuery();

                    string insertEQuery = "INSERT INTO EmotionalLevels (EnergyLevel, PainLevel) VALUES (" + EnergyLevel + ", " + PainLevel + ")";
                    MySql.Data.MySqlClient.MySqlCommand eCommand = new MySql.Data.MySqlClient.MySqlCommand(insertEQuery, frmMain.connectDB());
                    eCommand.ExecuteNonQuery();

                    string insertMQuery = "INSERT INTO Moods (MoodDescription, Date) VALUES(";
                    insertMQuery += "\'" + cbMoodChanges.Text + "\'" + ", ";
                    insertMQuery += "\'" + DateTime.Now.ToString("yyyy-MM-dd") + "\'" + ") ";
                    MySql.Data.MySqlClient.MySqlCommand mCommand = new MySql.Data.MySqlClient.MySqlCommand(insertMQuery, frmMain.connectDB());
                    mCommand.ExecuteNonQuery();
                    frmMain.disconnectDB();
                }
                catch (MySql.Data.MySqlClient.MySqlException mysqlex)
                {
                    MessageBox.Show("There was an error while trying to connect to the database: " + mysqlex.Message);
                }
                catch (InvalidOperationException ioe)
                {
                    MessageBox.Show("There has been an invalid operation: " + ioe.Message);
                }
            }
            if (checkValidM() == false)
            {
                tabcontrolDailyLog.SelectTab(tabMealInfo);
            }
            if (checkValidS() == false)
            {
                tabcontrolDailyLog.SelectTab(tabSleepHabits);
            }
            if (checkLevels() == false)
            {
                tabcontrolDailyLog.SelectTab(tabEmotionalLevels);
            }
        }
    }
}
